package com.mofang.powerdekorhelper.activity.card;

import android.support.design.widget.TabLayout;
import android.view.View;
import android.view.ViewGroup;

import com.mofang.powerdekorhelper.R;
import com.mofang.powerdekorhelper.adapter.CardAdapter;
import com.mofang.powerdekorhelper.base.MvpActivity;
import com.mofang.powerdekorhelper.base.recycleview.BaseRecycleViewAdapter;
import com.mofang.powerdekorhelper.base.recycleview.BaseRecycleViewList;
import com.mofang.powerdekorhelper.model.Card;
import com.mofang.powerdekorhelper.model.CardType;
import com.mofang.powerdekorhelper.persenter.CardPerenster;
import com.mofang.powerdekorhelper.utils.LoadPrograss;
import com.mofang.powerdekorhelper.utils.T;
import com.mofang.powerdekorhelper.view.CardView;
import com.mofang.powerdekorhelper.witget.TitleBar;

import butterknife.BindView;

/**
 * Created by luofei on 2017/8/21 0021.
 */

public class CardActivity extends MvpActivity<CardView, CardPerenster> implements CardView, TabLayout.OnTabSelectedListener {

    @BindView(R.id.card_tab_layout)
    TabLayout tabLayout;

    @BindView(R.id.card_titlebar)
    TitleBar mTitleBar;

    @BindView(R.id.card_list)
    BaseRecycleViewList cardRecycleList;

    private LoadPrograss loadPrograss;

    CardAdapter adapter;

    @Override
    public void initView() {
        inflateLayout(R.layout.activity_card_layout);
        initTitle();
        loadPrograss = new LoadPrograss(this);
    }

    @Override
    public void initData() {

        CardType cardType = new CardType(getResources().getString(R.string.all_text), 0);
        tabLayout.addTab(tabLayout.newTab().setText(cardType.getTypeName())
                .setTag(cardType));

        cardType = new CardType(getResources().getString(R.string.card_redbag_text), 1);
        tabLayout.addTab(tabLayout.newTab().setText(R.string.card_redbag_text)
                .setTag(cardType));

        cardType = new CardType(getResources().getString(R.string.coupon_text), 2);
        tabLayout.addTab(tabLayout.newTab().setText(R.string.coupon_text)
                .setTag(cardType));

        cardType = new CardType(getResources().getString(R.string.discount_text), 3);
        tabLayout.addTab(tabLayout.newTab().setText(R.string.discount_text)
                .setTag(cardType));

        tabLayout.setOnTabSelectedListener(this);
        presenter.getCard("");
    }

    @Override
    public void onTabSelected(TabLayout.Tab tab) {

        CardType type = (CardType) tab.getTag();

        switch (type.getTypeId()) {

            case 0:
                presenter.getCard("");
                break;

            case 1:
                presenter.getCard("");
                break;

            case 2:
                presenter.getCard("");
                break;

            case 3:
                presenter.getCard("");
                break;
        }
    }

    @Override
    public void setCard(Card card) {

        adapter = new CardAdapter(card.getResult().getData(), this, R.layout.card_coupon_item_layout);
        adapter.setOnItemClickListner(new BaseRecycleViewAdapter.OnItemClickListner() {
            @Override
            public void onItemClickListner(ViewGroup parent, View v, int position) {
                toActivity(CardActivity.this, CardDetailActivity.class);
            }
        });
        cardRecycleList.setAdapter(adapter);

        hideProgress();
    }


    @Override
    public void showProgress() {

        loadPrograss.show();
    }

    @Override
    public void hideProgress() {
        loadPrograss.hide();
    }

    @Override
    public void toast(CharSequence s) {
        T.showShort(this, s);
    }

    @Override
    public void toast(int id) {

    }

    @Override
    public void showNullLayout() {

    }

    @Override
    public void hideNullLayout() {

    }

    @Override
    public void showErrorLayout(View.OnClickListener listener) {

    }

    @Override
    public void hideErrorLayout() {

    }

    @Override
    public void onError(String errorInfo) {
        hideProgress();
        toast(errorInfo);
    }

    @Override
    public CardPerenster initPresenter() {
        return new CardPerenster();
    }

    private void initTitle() {
        initTitleBarWithback(mTitleBar, R.string.card_title_text);
    }

    @Override
    public void onTabUnselected(TabLayout.Tab tab) {

    }

    @Override
    public void onTabReselected(TabLayout.Tab tab) {

    }
}
